<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="检索" prop="search">
        <el-input v-model="queryParams.search" placeholder="请输入检索信息" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
    
      <el-form-item label="交货日期" prop="deliveryTime">
        <el-date-picker clearable
          v-model="queryParams.deliveryTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择交货日期">
        </el-date-picker>
      </el-form-item>
      
      <el-form-item label="签订日期" prop="signDate">
        <el-date-picker clearable
          v-model="queryParams.signDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择签订日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="申请状态" prop="createStatus">
        <el-select v-model="queryParams.createStatus" placeholder="请选择申请状态">
          <el-option label="待审核" value="0"></el-option>
          <el-option label="已审核" value="1"></el-option>
          <el-option label="未提交" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="updateStatus">
        <el-select v-model="queryParams.updateStatus" placeholder="请选择审核状态">
          <el-option label="驳回" value="0"></el-option>
          <el-option label="审核通过" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['carbon:contract:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['carbon:contract:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['carbon:contract:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['carbon:contract:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contractList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="50" label="编号" />
      <el-table-column label="合同编号" align="center" prop="contractId" />
      <el-table-column label="订单编号" align="center" prop="orderId" />
      <el-table-column label="业务员" align="center" prop="salesman" />
      <el-table-column label="客户" align="center" prop="customerName" />
      <el-table-column label="交货日期" align="center" prop="deliveryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="送货方式" align="center" prop="deliveryHow" />
      <el-table-column label="合同金额" align="center" prop="contractAmount" />
      <el-table-column label="签订日期" align="center" prop="signDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请状态" align="center" prop="createStatus">
        <template slot-scope="scope">

        <el-tag effect="dark" type="danger" v-if="scope.row.createStatus == '0'">
          待审核
        </el-tag>
        <el-tag effect="dark" type="info" v-else-if="scope.row.createStatus == '1'">
          已审核
        </el-tag>
          <el-tag effect="dark" v-else>
          未提交
          </el-tag>
              </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="updateStatus">
        <template slot-scope="scope">

          <el-tag effect="dark" type="danger" v-if="scope.row.updateStatus == '0'">
            驳回
          </el-tag>
          <el-tag effect="dark" type="info" v-else-if="scope.row.updateStatus == '1'">
            审核通过
          </el-tag>
          <el-tag effect="dark" v-else>
            待审核
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['carbon:contract:edit']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改销售合同审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="合同编号" prop="contractId">
          <el-input v-model="form.contractId" placeholder="请输入合同编号" :disabled="this.form.createStatus!=null?true:false" />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单编号" :disabled="this.form.createStatus!=null?true:false" />
        </el-form-item>
        <el-form-item label="业务员" prop="salesman">
          <el-input v-model="form.salesman" placeholder="请输入业务员" :disabled="this.form.createStatus!=null?true:false"/>
        </el-form-item>
        <el-form-item label="客户" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户" :disabled="this.form.createStatus!=null?true:false"/>
        </el-form-item>
        <el-form-item label="交货日期" prop="deliveryTime">
          <el-date-picker clearable
            v-model="form.deliveryTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择交货日期" :disabled="this.form.createStatus!=null?true:false">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="送货方式" prop="deliveryHow">
          <el-input v-model="form.deliveryHow" placeholder="请输入送货方式" :disabled="this.form.createStatus!=null?true:false"/>
        </el-form-item>
        <el-form-item label="合同金额" prop="contractAmount">
          <el-input v-model="form.contractAmount" placeholder="请输入合同金额" :disabled="this.form.createStatus!=null?true:false"/>
        </el-form-item>
        <el-form-item label="签订日期" prop="signDate">
          <el-date-picker clearable
            v-model="form.signDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择签订日期" :disabled="this.form.createStatus!=null?true:false">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核意见" prop="comments" v-if="this.form.id!=null">
          <el-input type="textarea" v-model="form.comments" placeholder="请输入审核意见" :disabled="this.form.updateStatus!=null?true:false"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="this.form.createStatus == null" @click="submitForm">提 交</el-button>
        <el-button type="success" v-if="this.form.createStatus == '0'" @click="submitForm2">通 过</el-button>
        <el-button type="primary" v-if="this.form.createStatus == '0'" @click="submitForm3">驳 回</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContract, getContract, delContract, addContract, updateContract,updateStatus } from "@/api/task/contract";

export default {
  name: "Contract",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售合同审核表格数据
      contractList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractId: null,
        orderId: null,
        salesman: null,
        customerName: null,
        deliveryTime: null,
        deliveryHow: null,
        contractAmount: null,
        signDate: null,
        createStatus: null,
        updateStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询销售合同审核列表 */
    getList() {
      this.loading = true;
      listContract(this.queryParams).then(response => {
        this.contractList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        contractId: null,
        orderId: null,
        salesman: null,
        customerName: null,
        deliveryTime: null,
        deliveryHow: null,
        contractAmount: null,
        signDate: null,
        createBy: null,
        createTime: null,
        createStatus: null,
        updateBy: null,
        updateTime: null,
        updateStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加销售合同审核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContract(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改销售合同审核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.createStatus == null) {
            this.form.createStatus = '0'
          }
          if (this.form.id != null) {
            updateContract(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContract(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
     /** 通过按钮 */
     submitForm2 () {
      this.form.updateStatus = '1';
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateStatus(this.form.id, this.form.updateStatus, this.form.comments).then(response => {
            this.$modal.msgSuccess(response.msg);
            this.open = false;
            this.reset();
            this.getList();
          });
        }
      });
    },
    /** 驳回按钮 */
    submitForm3 () {
      this.form.updateStatus = '0';
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateStatus(this.form.id, this.form.updateStatus, this.form.comments).then(response => {
            this.$modal.msgSuccess(response.msg);
            this.open = false;
            this.reset();
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除销售合同审核编号为"' + ids + '"的数据项？').then(function() {
        return delContract(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('carbon/contract/export', {
        ...this.queryParams
      }, `contract_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
